import 'package:flutter/material.dart';

//void main() => runApp(MyApp());

class MyNotification extends StatelessWidget {
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'lufei',
        theme: ThemeData(
            primaryColor: Colors.blueAccent,
            brightness: Brightness.light,
            accentColor: Colors.black),
        home: MyHomePage());
  }
}

class MyHomePage extends StatefulWidget {
//  MyHomePage({Key key}) : super(key: key);
//
//  final String

  @override
  _MyHomePageState createState() => _MyHomePageState();
}


class _MyHomePageState extends State<MyHomePage> {
  String _msg = "通知：";
  @override
  Widget build(BuildContext context) {
    //监听通知
    return NotificationListener<CustomNotification>(
        onNotification: (notification) {
          setState(() {_msg += notification.msg+"  ";});//收到子Widget通知，更新msg
        },
        child:Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[Text(_msg),CustomChild()],//将子Widget加入到视图树中
        )
    );
  }
}


class CustomNotification extends Notification {
  CustomNotification(this.msg);

  final String msg;
}

class CustomChild extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return RaisedButton(
        onPressed: () => CustomNotification("Hi").dispatch(context),
        child: Text('咋瓦鲁多'));
  }
}
